<?php

class EmployeeResearchProjectForm extends sfForm
{

    private $employeeService;
    public $employeeResearchProjects;

    /**
     * Get EmployeeService
     * @returns EmployeeService
     */
    public function getEmployeeService()
    {
        if (is_null($this->employeeService)) {
            $this->employeeService = new EmployeeService();
            $this->employeeService->setEmployeeDao(new EmployeeDao());
        }
        return $this->employeeService;
    }

    /**
     * Set EmployeeService
     * @param EmployeeService $employeeService
     */
    public function setEmployeeService(EmployeeService $employeeService)
    {
        $this->employeeService = $employeeService;
    }

    public function configure()
    {
        $this->employeeResearchProjectPermissions = $this->getOption('employeeResearchProjectPermissions');

        $empNumber = $this->getOption('empNumber');

        $widgets = array('emp_number' => new sfWidgetFormInputHidden(array(), array('value' => $empNumber)));
        $validators = array('emp_number' => new sfValidatorString(array('required' => false)));

        if ($this->employeeResearchProjectPermissions->canRead()) {

            $employeeResearchProjectWidgets = $this->getEmployeeResearchProjectWidgets();
            $employeeResearchProjectValidators = $this->getEmployeeResearchProjectValidators();

            if (!($this->employeeResearchProjectPermissions->canUpdate() || $this->employeeResearchProjectPermissions->canCreate())) {
                foreach ($employeeResearchProjectWidgets as $widgetName => $widget) {
                    $widget->setAttribute('disabled', 'disabled');
                }
            }
            $widgets = array_merge($widgets, $employeeResearchProjectWidgets);
            $validators = array_merge($validators, $employeeResearchProjectValidators);
        }

        $this->setWidgets($widgets);
        $this->setValidators($validators);

        $this->employeeResearchProjects = $this->getEmployeeService()->getEmployeeResearchProjectRecords($empNumber);
        $this->widgetSchema->setNameFormat('employeeResearchProject[%s]');

//        $this->validatorSchema->setPostValidator(
//            new sfValidatorSchemaCompare('start_date', sfValidatorSchemaCompare::LESS_THAN, 'end_date',
//                array(),
//                array('invalid' => 'The start date must be before the end date')
//            )
//        );
    }

    /*
     * Tis fuction will return the widgets of the form
     */

    public function getEmployeeResearchProjectWidgets()
    {
        $widgets = array();

        //creating widgets
        $widgets['seqno'] = new sfWidgetFormInputHidden();
        $widgets['name'] = new sfWidgetFormInputText();
        $widgets['role'] = new sfWidgetFormInputText();
        $widgets['start_date'] = new ohrmWidgetDatePicker(array(), array('id' => 'employeeResearchProject_start_date'));
        $widgets['end_date'] = new ohrmWidgetDatePicker(array(), array('id' => 'employeeResearchProject_end_date'));
        $widgets['status'] = new sfWidgetFormInputText();

        return $widgets;
    }

    /*
     * Tis fuction will return the form validators
     */

    public function getEmployeeResearchProjectValidators()
    {
        $inputDatePattern = sfContext::getInstance()->getUser()->getDateFormat();

        $validators = array(
            'seqno' => new sfValidatorString(array('required' => false)),
            'name' => new sfValidatorString(array('required' => true, 'max_length' => 255)),
            'role' => new sfValidatorString(array('required' => true, 'max_length' => 50)),
            'start_date' => new ohrmDateValidator(array('date_format' => $inputDatePattern, 'required' => true), array('required' => 'Date field is required', 'invalid' => 'Date format should be ' . $inputDatePattern)),
            'end_date' => new ohrmDateValidator(array('date_format' => $inputDatePattern, 'required' => false), array('required' => 'Date field is required', 'invalid' => 'Date format should be ' . $inputDatePattern)),
            'status' => new sfValidatorString(array('required' => false, 'max_length' => 50)),
        );

        return $validators;
    }

}

?>